//
//  PickViewAlartView.m
//  KEREN
//
//  Created by MW on 2023/3/31.
//

#import "PickViewAlartView.h"

@implementation PickViewAlartView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title currentIndex:(int)currentIndex IsCanChooseHistory:(BOOL)isCanChooseHistory{
    self = [super initWithFrame:frame];
    if(self){
        self.title = title;
        self.currentIndex = currentIndex;
        self.isCanChooseHistory = isCanChooseHistory;
        if([self.title containsString:@"选择日期"]){
            NSDate *nowDate = [NSDate date];
            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
            
            _comps = [[NSDateComponents alloc] init];
            NSInteger unitFlags = NSCalendarUnitYear |NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitWeekday |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
            _comps = [calendar components:unitFlags fromDate:nowDate];
            _year = _comps.year;
            _month = _comps.month;
            _day = _comps.day;
            _yearRow = [KRAlartManager shareInstance].yearRow;
            _monthRow = [KRAlartManager shareInstance].monthRow;
            _dayRow = [KRAlartManager shareInstance].dayRow;
            NSInteger dayCount;
            if(_monthRow > 0){
                 dayCount = [[DeteManager shareInstance] monthDayWithYear:_year month:_monthRow + 1];
            }else{
                 dayCount = [[DeteManager shareInstance] monthDayWithYear:_year month:_month];
            }
            [self setDayArrwithDayCount:dayCount];
        }
        [self setViewLayout];
    }
    return self;
}

- (void)setViewLayout{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.mask = [CAShapeLayer clipsToBounds:self.bounds Corners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(25, 25)];
    UILabel *titleLbl = [[UILabel alloc] init];
    [self addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.top.offset(15);
        make.height.offset(30);
    }];
    //TODO: 国际化
    titleLbl.text = self.title;
    titleLbl.font = BFont(20);
    titleLbl.textColor = Black_Color;
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:closeBtn];
    [closeBtn setImage:[UIImage imageNamed:@"NavBar_Default_Close"] forState:UIControlStateNormal];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.width.height.offset(40);
        make.top.offset(10);
    }];
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *lineView = [UIView setLineViewWithframe:CGRectZero forinView:self];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(1);
        make.top.offset(59);
    }];
    _pickView = [[UIPickerView alloc] init];
    [self addSubview:_pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(60);
        make.bottom.offset(-(100 + BottomSpance_height));
        make.width.offset(DEVICE_WIDTH);
        make.left.offset(0);
    }];
    _pickView.delegate = self;
    _pickView.dataSource = self;
    if([self.title isEqualToString:@"蓝牙强度"]){
        _titleArr = @[@"强",@"中",@"弱"];
        if(_currentIndex < 0 || _currentIndex == 0){
            _currentIndex = 2;
        }else if(_currentIndex > 1){
            _currentIndex = 0;
        }
        _currentTitle = _titleArr[_currentIndex];
        [_pickView selectRow:_currentIndex inComponent:0 animated:YES];

    }else if([self.title containsString:@"失窃上报类型"]){
        _titleArr = @[@"车辆失窃",@"电池失窃"];
        _currentIndex = 0;
        _currentTitle = _titleArr[_currentIndex];
        [_pickView selectRow:_currentIndex inComponent:0 animated:YES];
    }
    else if ([self.title containsString:@"低电量设置"]){
        if(_currentIndex <= 10){
            _currentIndex = 0;
        }else if (_currentIndex > 10 && _currentIndex <= 20){
            _currentIndex = 1;
        }else if (_currentIndex > 20 && _currentIndex <= 30){
            _currentIndex = 2;
        }else if (_currentIndex > 30 && _currentIndex <= 40){
            _currentIndex = 3;
        }else if (_currentIndex > 40){
            _currentIndex = 4;
        }
        _titleArr = @[@"10",@"20",@"30",@"40",@"50"];
        _currentTitle = _titleArr[_currentIndex];
        [_pickView selectRow:_currentIndex inComponent:0 animated:YES];

    }else if([self.title containsString:@"离车自动锁定倒计时"]){
        _titleArr = @[@"60",@"120",@"180"];
        if(_currentIndex == 60){
            _currentIndex = 0;
        }else if(_currentIndex == 120){
            _currentIndex = 1;
        }else if(_currentIndex == 180){
            _currentIndex = 2;
        }else{
            _currentIndex = 0;
        }
        _currentTitle = _titleArr[_currentIndex];
        [_pickView selectRow:_currentIndex inComponent:0 animated:YES];
    }else if([self.title containsString:@"围栏半径"]){

        if(_currentIndex == 3000){
            _currentIndex = 0;
        }else if(_currentIndex == 5000){
            _currentIndex = 1;
        }else if(_currentIndex == 10000){
            _currentIndex = 2;
        }else if(_currentIndex == 30000){
            _currentIndex = 3;
        }
        else if (_currentIndex == 100000){
            _currentIndex = 4;
        }else{
            _currentIndex = 1;
        }
        _titleArr = @[@"3",@"5",@"10",@"30",@"100"];
        if(_currentIndex < 0){
            _currentIndex = 0;
        }
        _currentTitle = _titleArr[_currentIndex];
        [_pickView selectRow:_currentIndex inComponent:0 animated:YES];

    }else if([self.title containsString:@"选择日期"]){
        _yearArr = @[@"2023",@"2024",@"2025"];
        //_currentTitle
        if(_yearRow > 0){
            [_pickView selectRow:_yearRow inComponent:0 animated:YES];
        }else{
            [_pickView selectRow:0 inComponent:0 animated:YES];
        }
        _monthArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
        if(_monthRow > 0){
            [_pickView selectRow:_monthRow inComponent:1 animated:YES];
            _month = _monthRow + 1;
        }else{
            [_pickView selectRow:_comps.month - 1 inComponent:1 animated:YES];
        }
        if(_dayRow > 0){
            [_pickView selectRow:_dayRow inComponent:2 animated:YES];
            _day = _dayRow + 1;
        }else{
            [_pickView selectRow:_comps.day - 1 inComponent:2 animated:YES];
        }
        _currentTitle = [NSString stringWithFormat:@"%ld-%02ld-%ld",_comps.year,_comps.month,_comps.day];
    }
    //[pickView reloadAllComponents];
    UIButton *confirmBtn = [UIButton setButtonWithTitle:@"确认" titleColor:Whire_Color titleFont:BFont(16) cornerRadius:25 backColor:Color_7ACC00];
    [self addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.right.offset(-24);
        make.height.offset(58);
        make.bottom.offset(-BottomSpance_height);
    }];
    [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)setDayArrwithDayCount:(NSInteger)count{
    NSMutableArray *dayArrM = [NSMutableArray array];
    for (int i = 0; i < count; i ++) {
        [dayArrM addObject:[NSString stringWithFormat:@"%d日",i + 1]];
    }
    _dayArr = dayArrM;
}

- (void)close{
    self.changeTitleBlock(@"");
}
- (void)confirm{
    self.changeTitleBlock(_currentTitle);
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if([self.title containsString:@"选择日期"]){
        return 3;
    }else{
        return 1;
    }
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if([self.title containsString:@"选择日期"]){
        if(component == 0){
            return 3;
        }else if (component == 1){
            return 12;
        }else{
            return _dayArr.count;
        }
    }else{
        return _titleArr.count;

    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 56;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if([self.title containsString:@"选择日期"]){
        return DEVICE_WIDTH/3;
    }else{
        return DEVICE_WIDTH;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if([self.title containsString:@"选择日期"]){
        if(component == 0){
            if([_yearArr[row] intValue] > _year && _isCanChooseHistory){
                [_pickView selectRow:0 inComponent:0 animated:YES];
                [_pickView reloadComponent:0];
            }else{
                _year = [_yearArr[row] intValue];
                [KRAlartManager shareInstance].yearRow = row;
                _yearRow = row;
            }
        }else if (component == 1){
            if([_monthArr[row] intValue] > _comps.month && _isCanChooseHistory){
                _month = _comps.month;
                [KRAlartManager shareInstance].monthRow = _comps.month - 1;
                [_pickView selectRow:_comps.month - 1 inComponent:1 animated:YES];
                [_pickView reloadComponent:1];
                if(_day > _comps.day){
                    _day = _comps.day;
                    [KRAlartManager shareInstance].dayRow = _comps.day - 1;
                    [_pickView selectRow:_comps.day - 1 inComponent:2 animated:YES];
                    [_pickView reloadComponent:2];
                }
            }else{
                if(!_isCanChooseHistory && _year == _comps.year && _month < [_monthArr[row] intValue]){
                    [KRAlartManager shareInstance].monthRow = _comps.month - 1;
                    [_pickView selectRow:_comps.month - 1 inComponent:1 animated:YES];
                    [_pickView reloadComponent:1];
                    if(_day < _comps.day){
                        _day = _comps.day;
                        [KRAlartManager shareInstance].dayRow = _comps.day - 1;
                        [_pickView selectRow:_comps.day - 1 inComponent:2 animated:YES];
                        [_pickView reloadComponent:2];
                    }
                }else{
                    _month = [_monthArr[row] intValue];
                    if(_month == _comps.month){
                        if(_day > _comps.day){
                            _day = _comps.day;
                            [KRAlartManager shareInstance].dayRow = _comps.day - 1;
                            [_pickView selectRow:_comps.day - 1 inComponent:2 animated:YES];
                            [_pickView reloadComponent:2];
                        }
                    }
                    _monthRow = row;
                    [KRAlartManager shareInstance].monthRow = row;
                    NSInteger dayCount = [[DeteManager shareInstance] monthDayWithYear:_year month:_month];
                    [self setDayArrwithDayCount:dayCount];
                    [_pickView reloadComponent:2];
                }
            }
        }else{
            if([_dayArr[row] intValue] > _comps.day && _isCanChooseHistory && _month == _comps.month){
                _day = _comps.day;
                [KRAlartManager shareInstance].dayRow = _comps.day - 1;
                [_pickView selectRow:_comps.day - 1 inComponent:2 animated:YES];
                [_pickView reloadComponent:2];
            }else{
                if(!_isCanChooseHistory && _month == _comps.month && _day < [_dayArr[row] intValue]){
                    _day = _comps.day;
                    [KRAlartManager shareInstance].dayRow = _comps.day - 1;
                    [_pickView selectRow:_comps.day - 1 inComponent:2 animated:YES];
                    [_pickView reloadComponent:2];
                }else{
                    _day = [_dayArr[row] intValue];
                    [KRAlartManager shareInstance].dayRow = row;
                    _dayRow = row;
                }
            }
        }
        _currentTitle = [NSString stringWithFormat:@"%ld-%02ld-%02ld",_year,_month,_day];
    }else{
        _currentTitle = _titleArr[row];
    }
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if([self.title containsString:@"选择日期"]){
        if(component == 0){
            return _yearArr[row];
        }else if (component == 1){
            return [NSString stringWithFormat:@"%@月",_monthArr[row]];
        }else {
            return _dayArr[row];
        }
    }else{
        return _titleArr[row];
    }
}

-(UIView *)backView{
    if(!_backView){
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewendEditing)];
        [_backView addGestureRecognizer:tap];
        _backView.userInteractionEnabled = YES;
        _backView.backgroundColor = rgba(0, 0, 0, 0.6);
    }
    return _backView;
}

- (void)viewendEditing{
    [self endEditing:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

@end
